package com.qqtech.qquan.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.user.dao.UserIdentityDao;
import com.qqtech.qquan.user.enums.UserIdentityTypeDictEnum;
import com.qqtech.qquan.user.model.UserIdentity;
import com.qqtech.qquan.user.model.UserIdentityVo;

@Service
public class UserIdentityServiceImpl extends BaseServiceImpl<UserIdentity>implements UserIdentityService {
	public static final Logger log = LoggerFactory.getLogger(UserIdentityServiceImpl.class);

	@Resource
	private UserIdentityDao userIdentityDao;

	@Override
	protected BaseDao<UserIdentity> getBaseDao() {
		return userIdentityDao;
	}

	@Override
	public String getPhoneByUserId(Integer userId) {
		UserIdentity userIdentity = new UserIdentity();
		userIdentity.setUserId(userId);
		userIdentity.setType(UserIdentityTypeDictEnum.PHONE.getCode());
		List<UserIdentity> listUserIdentity = userIdentityDao.queryList(userIdentity);
		if (listUserIdentity != null && listUserIdentity.size() > 0) {
			return listUserIdentity.get(0).getIdentity();
		}
		return "";
	}

	@Override
	public UserIdentityVo getIdentityByType(java.lang.String identity, UserIdentityTypeDictEnum identityType) {
		return userIdentityDao.getIdentityByType(identity, identityType);
	}

	@Override
	public UserIdentityVo getPhoneIdentity(java.lang.String identity) {
		return userIdentityDao.getPhoneIdentity(identity);
	}

	@Override
	public boolean isDuplicate(String identity, UserIdentityTypeDictEnum identityType) {
		if (StringUtil.isBlank(identity)) {
			return true;
		}
		if (identityType == null) {
			identityType = UserIdentityTypeDictEnum.PHONE;
		}
		UserIdentityVo userIdentityVo = this.getIdentityByType(identity, identityType);
		if (userIdentityVo != null && identityType.getCode() == userIdentityVo.getType()) {
			return true;
		}
		return false;
	}
}